package com.cosmic.pizza.service.Impl;

import com.cosmic.pizza.dao.FoodDao;
import com.cosmic.pizza.dao.Impl.FoodDaoImpl;
import com.cosmic.pizza.pojo.Food;
import com.cosmic.pizza.pojo.PageBean;
import com.cosmic.pizza.service.FoodService;

import java.util.List;

public class FoodServiceImpl implements FoodService {
    private FoodDao foodDao = new FoodDaoImpl();
    @Override
    //根据种类查询全部(分页)
    public PageBean<Food> findByType(int type, int currentPage, int pageSize) {
        PageBean<Food> pageBean = new PageBean<>();
        pageBean.setCurrentPage(currentPage);
        pageBean.setPageSize(pageSize);
        //根据种类查询总数
        int totalCount = foodDao.countByType(type);
        pageBean.setTotalCount(totalCount);
        //查询页数
        int totalPage = (totalCount + pageSize - 1)/pageSize;
        pageBean.setTotalPage(totalPage);
        //查询页面食物信息
        int start = (currentPage - 1 )*pageSize;
        List<Food> list = foodDao.findByType(start,pageSize,type);
        for (int i = 0; i < list.size(); i++) {
            Integer fid = list.get(i).getFid();
            list.get(i).setFood_img(foodDao.findImg(fid));
        }
        pageBean.setList(list);
        return pageBean;
    }
    //根据fid查询单个Food信息
    @Override
    public Food findByFid(Integer fid) {
        Food food  = foodDao.findByFid(fid);
        food.setFood_img(foodDao.findImg(fid));
        return food;
    }
    //模糊查询显示内容(分页)
    @Override
    public PageBean<Food> findByFname(int currentPage, int pageSize, String fname) {
        PageBean<Food> pageBean = new PageBean<>();
        pageBean.setPageSize(pageSize);
        pageBean.setCurrentPage(currentPage);
        //查询总数
        int totalCount = foodDao.countByFname(fname);
        pageBean.setTotalCount(totalCount);
        //查询页数
        int totalPage = (totalCount + pageSize - 1)/pageSize;
        pageBean.setTotalPage(totalPage);
        //查询页面食物信息
        int start = (currentPage - 1 )*pageSize;
        List<Food> list = foodDao.findByFname(start,pageSize,fname);
        for (int i = 0; i < list.size(); i++) {
            Integer fid = list.get(i).getFid();
            list.get(i).setFood_img(foodDao.findImg(fid));
        }
        pageBean.setList(list);
        return pageBean;
    }

    @Override
    public void updateEval(int fid, String time, int evalsum, String sum) {
        foodDao.updateEval(fid,  time,  evalsum,  sum);
    }


}
